---
title: 'Deploying Stagehand'
description: 'Deploy your AI agents and automations to the cloud'
---

<Tip>
**🌟 Preview: Browser Functions** - Deploy your web automation code directly on Browserbase with browser functions. Scale your `act()` automations in the cloud with zero infrastructure setup. Reach out to hello@browserbase.com to get beta access.
</Tip>

## Deploy on Vercel

Securely run Stagehand on Browserbase inside a Vercel Function. This guide shows a minimal, production-safe HTTP endpoint you can call directly or on a schedule.

### 1. Install Vercel CLI

To download and install Vercel CLI, run one of the following commands:

<CodeGroup>
```bash pnpm
pnpm i -g vercel
```
```bash yarn
yarn global add vercel
```
```bash npm
npm i -g vercel
```
```bash bun
bun add -g vercel
```
</CodeGroup>

### 2. Project layout

```text
your-project/
  api/
    run.ts
  package.json
  tsconfig.json
  vercel.json
```

Create the structure with:

```bash
mkdir -p api
touch api/run.ts package.json vercel.json tsconfig.json
```

### 3. `api/run.ts` (Node.js runtime)

```typescript
// api/run.ts
import type { VercelRequest, VercelResponse } from "@vercel/node";
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";

export default async function handler(req: VercelRequest, res: VercelResponse): Promise<void> {
  try {
    const stagehand = new Stagehand({
      env: "BROWSERBASE",
      apiKey: process.env.BROWSERBASE_API_KEY!,
      projectId: process.env.BROWSERBASE_PROJECT_ID!,
      modelName: "google/gemini-2.5-flash",
      modelClientOptions: {
        apiKey: process.env.GOOGLE_API_KEY!,
      },
      // optional session params
      browserbaseSessionCreateParams: {
        projectId: process.env.BROWSERBASE_PROJECT_ID!,
        region: "us-west-2",
        browserSettings: {
          blockAds: true,
        },
      },
    });

    await stagehand.init();
    const page = stagehand.page;

    await page.goto("https://www.stagehand.dev/");
    await page.act("click the evals button");

    const { extraction } = await page.extract("extract the fastest model");
    const data = { model: extraction ?? "" };

    await stagehand.close();

    res.status(200).json({ ok: true, data: data.model });
  } catch (err: unknown) {
    const msg = err instanceof Error ? err.message : String(err);
    res.status(500).json({ ok: false, error: msg });
  }
}
```

### 4. `package.json`

```json
{
    "name": "bb-stagehand-on-vercel",
    "private": true,
    "type": "module",
    "engines": { "node": ">=18" },
    "dependencies": {
      "@browserbasehq/stagehand": "^2.4.3",
      "zod": "^3.25.0"
    },
    "devDependencies": {
      "typescript": "^5.6.0",
      "@types/node": "^20.12.12",
      "@vercel/node": "^3.2.20"
    }
}
```

### 5. `tsconfig.json`

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "node",
    "outDir": ".vercel/output/functions",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "types": ["node"]
  },
  "include": ["api/**/*.ts"]
}
```

### 6. `vercel.json`

```json
{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "functions": {
    "api/run.ts": {
      "maxDuration": 60
    }
  }
}
```

See Vercel's [configuring functions](https://vercel.com/docs/functions/configuring-functions) docs for more details.

### 7. Link your project

Link your local folder to a Vercel project before configuring environment variables:

```bash
# authenticate if needed
vercel login

# link the current directory to a Vercel project (interactive)
vercel link
```

### 8. Environment variables

Do not commit `.env` in production. Add variables via Vercel CLI:

```bash
vercel env add BROWSERBASE_API_KEY
vercel env add BROWSERBASE_PROJECT_ID
# (and your model key if needed)
vercel env add GOOGLE_API_KEY
```

See also: [Browser Environment](/configuration/environment) for details on required variables.

### 9. Test locally

Replicate the Vercel environment locally to exercise your Function before deploying. Run from the project root.

```bash
# ensure dependencies are installed
npm install

# start the local Vercel dev server
vercel dev --listen 5005
```

### 10. Deploy

```bash
vercel
vercel --prod
```

### Execute the function

```bash
curl -X POST https://<your-deployment>/api/run
```

### Optional: Cron on Vercel

Hit the same endpoint on a schedule by extending `vercel.json`:

```json
{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "functions": {
    "api/run.ts": {
      "maxDuration": 60
    }
  }
  },
  "crons": [
    { "path": "/api/run", "schedule": "0 * * * *" }
  ]
}
```

### Features
- **No local browsers needed** with `env: "BROWSERBASE"`. [Browserbase](https://www.browserbase.com/) provides the browsers.
- **Fast functionality**: Offload browser work to Browserbase and return JSON promptly.
- **Long-running tasks**: Raise `maxDuration` and/or consider Edge runtime limits depending on plan.

